requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    var url = {
        selectRoleList: "manager/selectRoleList.do",//获取所有的角色
        cascade: "manager/departmentCascade.do"

    }
    var frontData = {
        pageNum: 1
    };
    $(".winBoxAdd").hide();
    $(".winBoxAdd").css("z-index", 0)
    $(".winBoxAdd_gray,.winBoxDelect_gray,.winBoxReset_gray").css({
        "position": "absolute",
        "top": 0,
        "left": 0,
        "width": "100%",
        "height": "100%",
        "z-index": 0,
        "background-color": "rgba(0,0,0,0.5)",
        "display": "none"
    });

    $(".add_man").eq(1).hide();
    $(".add_man").eq(2).hide();
    $(".add_man").eq(3).hide();

    get_role();

    function send(parentId) {
        site.send({
            url: url.cascade,
            //async:false
            data: {
                parentId: parentId,
            },
            callback: function (data) {
                data = JSON.parse(data)

                var source = $("#add_content_top_tpl").html();
                var template = Handlebars.compile(source);

                var html = template(data);
                $('#add_content_top').html(html);


                if (data != "") {


                } else {
                    $("#add_content_top").hide();
                }

                $("#add_content_top").on('change', function () {

                    sendTow($(this).val())
                })
            }
        })


    }

    function sendTow(parentId) {

        site.send({
            url: url.cascade,
            //async:false
            data: {
                parentId: parentId,
            },
            callback: function (data) {
                data = JSON.parse(data)

                var source = $("#add_content_top_tpl2").html();
                var template = Handlebars.compile(source);

                var html = template(data);
                $('#add_content_top2').html(html);


                if (data != "") {
                    $("#add_content_top2").show();
                } else {

                    $("#add_content_top3").hide();
                    $("#add_content_top4").hide();
                }

                $("#add_content_top2").on('change', function () {

                    sendThree($(this).val())
                })
            }
        })
    }

    function sendThree(parentId) {

        site.send({
            url: url.cascade,
            //async:false
            data: {
                parentId: parentId,
            },
            callback: function (data) {
                data = JSON.parse(data)

                var source = $("#add_content_top_tpl3").html();
                var template = Handlebars.compile(source);

                var html = template(data);
                $('#add_content_top3').html(html);


                if (data != "") {

                    $("#add_content_top3").show();



                } else {

                    $("#add_content_top4").hide();
                }

                $("#add_content_top3").on('change', function () {

                    sendFore($(this).val())
                })
            }
        })
    }

    function sendFore(parentId) {

        site.send({
            url: url.cascade,
            //async:false
            data: {
                parentId: parentId,
            },
            callback: function (data) {
                data = JSON.parse(data)

                var source = $("#edit_content_top_tpl4").html();
                var template = Handlebars.compile(source);

                var html = template(data);
                $('#add_content_top4').html(html);


                if (data != "") {
                    $("#add_content_top4").show();

                } else {
                    $("#add_content_top4").hide();

                }
                $("#add_content_top4").on('change', function () {

                })
            }
        })
    }

    function get_role() {
        //获取所有角色
        site.send({
            url: url.selectRoleList,
            //async:false
            data: frontData,
            callback: function (data) {
                data = JSON.parse(data)
                var source = $("#add_content_top_tpl5").html();
                var template = Handlebars.compile(source);
                var list = data.content;
                var html = template(list);
                $('#add_content_top5').html(html);
                $('#add_content_top5').show();
            }
        })
    }

    // 新增弹框
    $("#addUser").click(function () {
        $(".winBoxAdd").toggle();
        $(".winBoxAdd_gray").css("display", "block")
        send(-1);
        site.send({
            url: 'manager/buildAdminUsername.do',
            //async:false
            callback: function (data) {
                data = JSON.parse(data)
                $("#addEmpno").val(data.content);
            }
        })

    })
//新增提交
    var add_text,add_d_name;
    $(".addBtn").click(function () {
        // alert( $("#edit_content_top option:selected").text());
          add_text = $("#add_content_top4").val();
        add_d_name=$("#add_content_top4 option:selected").text();
 if(add_text==null|| !add_text){

     add_text = $("#add_content_top3").val()
     add_d_name = $("#add_content_top3 option:selected").text();

 }

        site.send({
            url: 'manager/insertAdminInfo.do',
            //async:false
            data: {

                username: $("#addEmpno").val(),
                realname: $("#addRealName").val(),
                telephone: $("#addTelephone").val(),

                empno: $("#addUserName").val(),
                mail: $("#addMail").val(),
                departmentId: add_text,
                roleId: $("#add_content_top5").val(),
                d_name:add_d_name
            },
            callback: function (data) {
                data = JSON.parse(data);
                Constant.fn.Message("F",data.msg)
                if (data.type == 1) {
                    $(".winBoxAdd").hide();
                    $(".winBoxAdd_gray").hide();
                    pjaxReload();
                }

            }
        })

    })

})
